package com.web;

import com.pojo.Book;
import com.pojo.Page;
import com.service.bookService;
import com.service.impl.bookServiceImpl;
import com.utils.webUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class clientbookServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        bookServlet b = new bookServlet();
        doPost(req,resp);
//    String action =  req.getParameter("action");
//    System.out.println("action===="+action);
//    Method method = null;
//    try {
//        method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
//        method.invoke(this, req, resp);
//    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//        e.printStackTrace();
//    }
    }
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        User u = new User();
//        try {
//            BeanUtils.populate(u,req.getParameterMap());
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        String action =  req.getParameter("action");
        Method method = null;
        try {
            method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    bookService book = new bookServiceImpl();
    private void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pageSize = req.getParameter("pageSize");
        int i =   webUtils.parseInt(pageSize,Page.PAGE_SIZE);
        String pageNo = req.getParameter("pageNo");
        int i1= webUtils.parseInt(pageNo,1);
        Page<Book> page =  book.pages(i1,i);
        page.setUrl("clientbook/page?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/项目/index.jsp").forward(req,resp);
    }
    private void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int max=webUtils.parseInt( req.getParameter("max"),Integer.MAX_VALUE);
        int min=webUtils.parseInt( req.getParameter("min"),0);
        int pageNo=webUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize=webUtils.parseInt(req.getParameter("pageSize"),Page.PAGE_SIZE);
       Page<Book> page =  book.pageByPrice(max,min,pageNo,pageSize);
       StringBuilder sb = new StringBuilder("clientbook/page?action=pageByPrice");
       if(req.getParameter("max")!=null){
                sb.append("&max=").append(req.getParameter("max"));
       }
        if(req.getParameter("min")!=null){
            sb.append("&min=").append(req.getParameter("min"));
        }
       page.setUrl(sb.toString());
        req.setAttribute("page",page);
        req.getRequestDispatcher("/项目/index.jsp").forward(req,resp);

    }
}
